package cc.telecomdigital.tdstock.Tools;

import cc.telecomdigital.tdstock.R;

import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Tools.Abstract_ActivityController;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;

// The owner should be a never ending object
// The activity must be a unique activity.  That is only possible to perform one job
// Allow for inheritance with any activity base class
public class ActivityController extends Abstract_ActivityController
{
	protected Context	ownerContext;
	// Default Animation parameter
	protected boolean	transitAnimation;
	// Forward to next Page GUI, Transition animation
	protected int		nextRInAnimation;
	protected int		nextROutAnimation;

	// Backward to previous Page, Transition animation
	protected int		backRInAnimation;
	protected int		backROutAnimation;
	
	private int   defaultIntentFlag;
	private final int defaultNextRInAnimation = R.anim.slide_in_right;
	private final int defaultNextROutAnimation = R.anim.slide_out_left;

	private final int defaultBackRInAnimation = R.anim.slide_in_left;
	private final int defaultBackROutAnimation = R.anim.slide_out_right;	

	private   IListIndexEndInvoke lastInvoker;
	
	public interface IListIndexEndInvoke
	{
		void Controller_AtLastIndex(Object iObj);
	}
	
	/** Owner could be anything, activity, Context **/
	public ActivityController(Context iOwnerContext, IListIndexEndInvoke iLastInvoker)
	{
		super();
		ownerContext = iOwnerContext;
		lastInvoker   = iLastInvoker;
		defaultIntentFlag = (Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_CLEAR_TOP);
		//defaultIntentFlag |= Intent.FLAG_ACTIVITY_NEW_TASK;		
		//defaultIntentFlag |= Intent.FLAG_ACTIVITY_NO_USER_ACTION;
		RestoreToDefaultAnimation();
	}

	/** Change the animation result to the default value,
	 *  User could manipulate this inner class to set the default animation
	 */
	public void RestoreToDefaultAnimation()
	{
		transitAnimation = false;
		nextRInAnimation = defaultNextRInAnimation;
		nextROutAnimation = defaultNextROutAnimation;

		backRInAnimation  = defaultBackRInAnimation;
		backROutAnimation = defaultBackROutAnimation;
	}
	
	@Override
	protected String GetModuleIdentity()
	{
		return "ActivityController";
	}
	
	/** Transfer ownership **/
	public ActivityController(ActivityController iImport, Context iNewOwner, IListIndexEndInvoke iLastInvoker)
	{
		super(iImport);
		ownerContext = iNewOwner;
		lastInvoker = iLastInvoker;
	}

	/** make copy of itself **/
	public ActivityController(ActivityController iImport)
	{
		super(iImport);
		ownerContext = iImport.ownerContext;
		transitAnimation = iImport.transitAnimation;
		nextRInAnimation = iImport.nextRInAnimation;
		nextROutAnimation = iImport.nextROutAnimation;

		backRInAnimation = iImport.backRInAnimation;
		backROutAnimation = iImport.backROutAnimation;
		lastInvoker = iImport.lastInvoker;
	}

	/** Set a last call back invoker, when list of index reach to the end **/
	public void SetLastInvoker(IListIndexEndInvoke iLastInvoker)
	{
		lastInvoker = iLastInvoker;
	}
	
	/** Are we are using Animation **/
	public boolean IsTransitWithAnimation()
	{
		return transitAnimation;
	}
	
	/** Set whether the animation is required **/
	public void TransitWithAnimation(boolean iEnable)
	{
		transitAnimation = iEnable;
	}

	/** Customize the directional animation resource for changing to next **/
	public void SetForwardAnimation(int iPageInAnimate, int iPageOutAnimate)
	{
		nextRInAnimation = iPageInAnimate;
		nextROutAnimation = iPageOutAnimate;
	}

	/** Customize the directional animation resource for changing to back **/
	public void SetBackwardAnimation(int iPageInAnimate, int iPageOutAnimate)
	{
		backRInAnimation = iPageInAnimate;
		backROutAnimation = iPageOutAnimate;
	}

	/** This would just go back to the previous list selection, the intent
	   source and destination will be modified **/
	@Override
	public boolean ActivityBackwardSwitch(Activity iCallActivity,
			Intent iIntentObj)
	{
		synchronized (listLock)
		{
			Class<? extends Activity> toActivityClass;

			if (debugMode)
				TDStockLog.i(LOG_TAG, "ActivityBackwardSwitch: Current Index = " + indexOfCurActivity);			
			// We are at the root, nothing we can change
			if (indexOfCurActivity <= 0)
			{
				//Clean self
				ActivityClearList();
				if (IsDestroyOnFinish())
					iCallActivity.finish();
				//Inform interest party, that we are at the end
				if (lastInvoker != null)
					lastInvoker.Controller_AtLastIndex(this);
				return true;
			}
			
			// Get Activity and shorten the list
			toActivityClass = activityClassList.get(indexOfCurActivity - 1);

			ActivitySwitchPerform(iCallActivity, toActivityClass, iIntentObj,
							 CHANGE_DIRECTION.BACK);
			try
			{
				activityClassList = activityClassList.subList(0, indexOfCurActivity);
			}
			catch(Exception e)
			{
				TDStockLog.e(LOG_TAG, "ActivityBackwardSwitch: " + e.toString());
				e.printStackTrace();
			}
			indexOfCurActivity = activityClassList.size() - 1;
			return true;
		}
	}	
	
	@Override
	protected void ActivitySwitchPerform(Activity iCallActivity,
			Class<? extends Activity> iChangingActClass, Intent iIntentObj,
			CHANGE_DIRECTION iSwitchDir)
	{
		// We want to get rid of the tab activity, not the activity itself
		Activity parentActivity;
		int      inAnimate, outAnimate;
		
		parentActivity = null;
		try
		{
			parentActivity = iCallActivity.getParent();
			if (parentActivity != null)
				iCallActivity = parentActivity;
		}
		catch(Exception ex)
		{
			TDStockLog.e(LOG_TAG, "The activity does not have parent");
		}

		iIntentObj.setClass(ownerContext, iChangingActClass);
		
		if (defaultIntentFlag != 0)
			iIntentObj.addFlags(defaultIntentFlag);

		if (transitAnimation)
		{
			if (iSwitchDir == CHANGE_DIRECTION.BACK)
			{
				inAnimate = backRInAnimation;
				outAnimate = backROutAnimation;				
			}				
			else
			{
				inAnimate = nextRInAnimation;
				outAnimate = nextROutAnimation;				
			}				
		}
		else
		{
			inAnimate = 0;
			outAnimate = 0;
		}		
		
		iCallActivity.startActivity(iIntentObj);					
		iCallActivity.overridePendingTransition(inAnimate,outAnimate);			
			
		if (IsDestroyOnFinish())
			iCallActivity.finish();	
	}
}
